package com.jeecg.p3.system.poster.service;

import com.jeecg.p3.system.poster.dao.PosterDao;
import com.jeecg.p3.system.poster.entity.Poster;
import org.jeecgframework.p3.core.utils.common.PageList;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.utils.common.PageQueryWrapper;
import org.jeecgframework.p3.core.utils.common.Pagenation;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PosterService {
    @Resource
    private PosterDao posterDao;

    /**
     * 分页查询海报信息
     * @param pageQuery
     * @return
     */
    public PageList<Poster> queryPageList(PageQuery<Poster> pageQuery) {
        PageList<Poster> result = new PageList<Poster>();
        Integer itemCount = posterDao.count(pageQuery);
        PageQueryWrapper<Poster> wrapper = new PageQueryWrapper<Poster>(pageQuery.getPageNo(), pageQuery.getPageSize(),itemCount, pageQuery.getQuery());
        List<Poster> list = posterDao.queryPageList(wrapper);
        Pagenation pagenation = new Pagenation(pageQuery.getPageNo(), itemCount, pageQuery.getPageSize());
        result.setPagenation(pagenation);
        result.setValues(list);
        return result;
    }

    /**
     * 新增海报信息
     * @param poster
     */
    @Transactional(rollbackFor = {Exception.class})
    public void doAdd(Poster poster) {
        poster.setStatus("1");
        poster.setIsNew("1");
        posterDao.insert(poster);
    }

    /**
     * 更新海报不为最新海报
     * @param poster
     */
    public void updateIsOld(Poster poster) {
        poster.setIsNew("0");
        posterDao.updateIsOld(poster);
    }

    /**
     * 根据id查询海报信息
     * @param id
     * @return
     */
    public Poster findById(Long id) {
        return posterDao.get(id);
    }

    /**
     * 根据海报编码查询海报信息
     * @param posterNo
     * @return
     */
    public Poster findByNo(String posterNo) {
        return posterDao.findByNo(posterNo);
    }

    public List<Poster> findByParam(Poster poster) {
        return posterDao.findByParam(poster);
    }

    /**
     * 编辑海报信息（）
     * @param poster
     */
    @Transactional(rollbackFor = {Exception.class})
    public void doEdit(Poster poster) {
        posterDao.update(poster);
    }

    /**
     * 删除海报信息
     * @param poster
     */
    @Transactional(rollbackFor = {Exception.class})
    public void doDelete(Poster poster) {
        poster.setStatus("0");
        posterDao.update(poster);
    }
}
